package util;

import java.io.*;
import java.util.Properties;

public class AccountMemoryUtil {
    private static final String FILE = "account_memory.properties";

    public static void saveAccount(String username, String password, String role) {
        Properties prop = new Properties();
        prop.setProperty("username", username);
        prop.setProperty("password", password);
        prop.setProperty("role", role);
        try (FileOutputStream out = new FileOutputStream(FILE)) {
            prop.store(out, "Saved Login Info");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String[] loadAccount(String role) {
        Properties prop = new Properties();
        try (FileInputStream in = new FileInputStream(FILE)) {
            prop.load(in);
            if (role.equals(prop.getProperty("role"))) {
                return new String[]{prop.getProperty("username"), prop.getProperty("password")};
            }
        } catch (IOException e) {
            // ignore
        }
        return new String[]{"", ""};
    }
}
